package shop.controller.manage.account;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.BaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.core.common.bean.ManageContainer;
import shop.core.common.dao.page.PagerModel;
import shop.services.manage.system.bean.User;
import shop.services.manage.system.impl.UserService;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户管理
 * @类名称：AccountAction
 * @创建人：Ltz
 * @创建时间：2017-09-20 下午15:33:04
 * @版本号：1.0
 * @描述：
 */
@Controller
@RequestMapping("/manage/account/")
public class AccountAction extends BaseController<User> {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(AccountAction.class);

    private static final String page_toList = "/manage/account/accountList";
    private static final String page_toMailList = "/manage/account/MailList";
    private static final String page_toRankManage = "/manage/account/rankManage";

    public AccountAction() {
        super.page_toEdit = null;
        super.page_toList = page_toList;
        super.page_toAdd = null;
    }

    @Autowired
    private UserService userService;

    @Override
    public Services<User> getService() {
        return userService;
    }

    /**
     * 跳转通讯录页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("toMailList")
    public String toMailList(HttpServletRequest request, @ModelAttribute("e") User e) throws Exception {
        e.clear();
        return page_toMailList;
    }

    /**
     * 跳转通讯录页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("toRankManage")
    public String toRankManage(HttpServletRequest request, @ModelAttribute("e") User e) throws Exception {
        e.clear();
        return page_toRankManage;
    }

    /**
     * 单个删除用户信息
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "deleteByIdJson", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult deleteByIdJson(int id) throws Exception {
        JSONResult jsonResult = new JSONResult();
        userService.deleteById(id);
        jsonResult.setMessage("success");
        return jsonResult;
    }

    /**
     * 分页获取数据列表
     *
     * @param request 请求
     * @param e       对象参数
     * @return 分页数据模型
     */
    @RequestMapping("loadAccountData")
    @ResponseBody
    public PagerModel loadAccountData(HttpServletRequest request, User e) {
        int offset = 0;
        int pageSize = 10;
        if (request.getParameter("start") != null) {
            offset = Integer.parseInt(request.getParameter("start"));
        }
        if (request.getParameter("length") != null) {
            pageSize = Integer.parseInt(request.getParameter("length"));
        }
        if (offset < 0)
            offset = 0;
        if (pageSize < 0) {
            pageSize = 10;
        }
        e.setOffset(offset);
        e.setPageSize(pageSize);
        //获取非超级管理员的用户信息
        PagerModel pager = userService.selectAllPageList(e);
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

    /**
     * 分页获取数据列表
     *
     * @param request 请求
     * @param e       对象参数
     * @return 分页数据模型
     */
    @RequestMapping("loadMailList")
    @ResponseBody
    public PagerModel loadMailList(HttpServletRequest request, User e) {
        int offset = 0;
        int pageSize = 10;
        if (request.getParameter("start") != null) {
            offset = Integer.parseInt(request.getParameter("start"));
        }
        if (request.getParameter("length") != null) {
            pageSize = Integer.parseInt(request.getParameter("length"));
        }
        if (offset < 0)
            offset = 0;
        if (pageSize < 0) {
            pageSize = 10;
        }
        e.setOffset(offset);
        e.setPageSize(pageSize);
        //获取通讯录信息
        PagerModel pager = userService.selectMailList(e);
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }
}
